package com.abcnull.testcase;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class biyingTest {
    private WebDriver driver;
    private Map<String, Object> vars;
    JavascriptExecutor js;
    public String waitForWindow(int timeout) {
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Set<String> whNow = driver.getWindowHandles();
        Set<String> whThen = (Set<String>) vars.get("window_handles");
        if (whNow.size() > whThen.size()) {
            whNow.removeAll(whThen);
        }
        return whNow.iterator().next();
    }

    @Test(description = "必应首页_搜索测试", priority = 3)
    public void testSearch() {
        System.setProperty("webdriver.chrome.driver","D:/driver/1/chromedriver.exe");
        driver = new ChromeDriver();
        js = (JavascriptExecutor) driver;
        vars = new HashMap<String, Object>();
        driver.get("https://cn.bing.com/?mkt=zh-cn");
        driver.manage().window().setSize(new Dimension(1074, 816));
        driver.findElement(By.id("sb_form_q")).click();
        driver.findElement(By.id("sb_form_q")).sendKeys("自动化测试框架");
        driver.findElement(By.id("sb_form_q")).sendKeys(Keys.ENTER);
        vars.put("window_handles", driver.getWindowHandles());
        driver.findElement(By.linkText("自动化测试框架知识，读这一篇就够了！ - 知乎")).click();
        vars.put("win4072", waitForWindow(2000));
        driver.switchTo().window(vars.get("win4072").toString());
        driver.findElement(By.cssSelector(".Modal-closeIcon")).click();
    }
}
